import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { NzMessageService } from 'ng-zorro-antd'
import { STColumn, STData } from '@delon/abc'
import { ACLService } from '@delon/acl'
import { SettingsService } from '@delon/theme'
import { I18NService } from '@core'
import { ListBaseClass } from '@shared/classes/ListBaseClass'
import { ISearchSchema } from '@shared/interfaces/search'
import { ESearchType } from '@shared/enums'
import { openDownloadDialog } from 'app/utils/helpers'
import { BatchImportService } from '../batch-import.service'
import { EDateLimitEnum, BatchStatusTextMap } from '@shared/interfaces/common'
import { today } from 'app/utils/contraints'
import { IBatchQueryParams, IBatchImportResult } from '@shared/interfaces/batch-import'

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['../../../shared/styles/table.less'],
})
export class ListComponent extends ListBaseClass<IBatchImportResult, IBatchQueryParams> implements OnInit {
  data: IBatchImportResult[] = []
  columns: STColumn[] = []
  searchSchema: ISearchSchema[] = []
  customSchema: ISearchSchema[] = []
  BatchStatusTextMap = BatchStatusTextMap
  showButton: boolean
  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public settings: SettingsService,
    public aclService: ACLService,
    private batchService: BatchImportService,
  ) {
    super(msg, i18n, route, router, settings)
  }

  ngOnInit(): void {
    this.showButton = true
    this.initColumns()
    this.searchSchema = this.generateSearchSchema()
    this.handleSearch()
  }

  goNew(): void {
    this.router.navigate(['../new'], { relativeTo: this.route })
  }

  queryTableDataWithParams(): void {
    this.loading = true
    this.showButton = true
    this.batchService.getList(this.params).subscribe(this.renderList.bind(this), this.getListError.bind(this))
  }

  renderList(res): void {
    const { data, total } = res
    this.data = data
    this.total = total
    this.loading = false
    if (data && data.length > 0) {
      this.showButton = false
    }
  }
  /** 导出单条 */
  export(record: STData): void {
    const params = record.feedback
    this.goExport(params)
  }

  private goExport(params: string): void {
    this.batchService.getFeedBook(params).subscribe(res => {
      const { url } = res
      openDownloadDialog(url, '下载反馈表')
    })
  }

  download() {
    this.batchService.downSample(this.params).subscribe(res => {
      const { url } = res
      openDownloadDialog(url, '下载批导样表')
    })
  }

  private initColumns() {
    this.columns = [
      {
        type: 'link',
        title: '序号',
        i18n: 'BATCH.HISTORY.NO',
        className: 'text-center',
        width: '150px',
        fixed: 'left',
        render: 'number'
      },
      {
        title: '批导表名称',
        i18n: 'BATCH.IMPORT.NAME',
        index: 'fileName',
        width: '300px',
        className: 'text-center',
      },
      {
        title: '上传时间',
        i18n: 'BATCH.IMPORT.UALOAD.TIME',
        index: 'createdAt',
        width: '200px',
        type: 'date',
        className: 'text-center',
      },
      {
        title: '批导结果',
        i18n: 'BATCH.IMPORT.RESULT',
        index: 'result',
        width: '100px',
        className: 'text-center',
        render: 'result'
      },
      {
        title: '操作',
        i18n: 'GENERAL.INFO.OPERATION',
        width: '150px',
        fixed: 'right',
        className: 'text-center p-xs',
        buttons: [
          {
            text: '',
            icon: {
              iconfont: 'icon-download-twotone',
              type: 'iconfont',
            },
            click: this.export.bind(this),
          },
        ],
      },
    ]
  }

  private generateSearchSchema(): ISearchSchema[] {
    return [
      {
        label: 'BATCH.IMPORT.UALOAD.TIME',
        labelI18n: 'BATCH.IMPORT.UALOAD.TIME',
        index: ['createdAtFrom', 'createdAtTo'],
        type: ESearchType.dateRange,
        allowClear: false,
        dateLimitType: EDateLimitEnum.Month,
        value: [today, today],
      },
      {
        label: 'BATCH.IMPORT.NAME',
        labelI18n: 'BATCH.IMPORT.NAME',
        index: 'fileName',
        type: ESearchType.input,
        value: [],
      },
    ]
  }

  upload(): void {
    this.router.navigate(['../upload'], { relativeTo: this.route })
  }
}
